import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Filter;

/**
 * @author <a href="mailto:xingxiao@gtmap.cn">xingxiaofeng</a>
 * @version2.1 2021-05-13
 * @description
 */
public class Test3
{
    public static void main(String[] args){

        MongoClient mongoClient = new MongoClient("192.168.1.29", 37019 );
        // 连接到数据库
        MongoDatabase mongoDatabase = mongoClient.getDatabase("test1");
        //创建集合
       // mongoDatabase.createCollection("test1");
        //获取集合
        MongoCollection<Document> mongoCollection =   mongoDatabase.getCollection("test1") ;
        updateMany(mongoCollection);
    }
    public static void setDoc(MongoCollection<Document> mongoCollection){
        for (int i=0;i<10;i++) {
            Document doc=new Document();
            doc.append("name","xxf"+i);
            doc.append("age", 10+i);
            mongoCollection.insertOne(doc);
        }
    }
    public static void getDoc(MongoCollection<Document> collection) {
       FindIterable<Document> list =collection.find();
       for (Document document: list){
           System.out.println("name:"+document.get("name")+",age:"+document.get("age"));
       }
    }
    public static void insertMany(MongoCollection<Document> collection) {
         List<Document> docList=new ArrayList<Document>() ;
        for (int i=10;i<20;i++) {
            Document doc=new Document();
            doc.append("name","xxf"+i);
            doc.append("age", 10+i);
            docList.add(doc);
        }
        collection.insertMany(docList);
    }

    public static void updateMany(MongoCollection<Document> collection){
        collection.updateMany(Filters.eq("name","xxf1"), new Document("$set",new Document("name","name100")));
       FindIterable<Document> list= collection.find(Filters.eq("name","name100")) ;
        for (Document document:list) {
            System.out.println("name:"+document.get("name"));

        }
    }
}
